﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Edit_Picture
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //tang kich thuoc anh
        int sumW = 0;
        int sumH = 0;
        //giam kich thuoc anh
        int subW = 0;
        int subH = 0;
        //duong dan den anh
        string ofdname;
        //khai bao bitmap
        Bitmap bmp;
        //kich thuoc anh
        int width;
        int height;
        //color of picture
        Color p;

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png|bmp (*.bmp)|*.bmp";
            if(ofd.ShowDialog()==DialogResult.OK&&ofd.FileName.Length>0)
            {
                pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
                pictureBox1.Image = Image.FromFile(ofd.FileName);
            }
            ofdname = ofd.FileName;
        }

        private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "jpg (*.jpg)|*.jpg|png (*.png)|*.png|bmp (*.bmp)|*.bmp";
            if(sfd.ShowDialog()==DialogResult.OK&&sfd.FileName.Length>0)
            {
                pictureBox1.Image.Save(sfd.FileName);
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void grayscaleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load picture
             bmp = new Bitmap(ofdname);
            //get dimenssion
             width = bmp.Width;
             height = bmp.Height;
            
            
            //conver grayscale
            for(int y = 0;y<height;y++)
                for(int x=0;x<width;x++)
                {
                    //get picture value
                    p = bmp.GetPixel(x, y);
                    //extract pixel component ARGB
                    int a = p.A;
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;
                    //find avg
                    int avg = (r + g + b) / 3;
                    //set new value
                    bmp.SetPixel(x, y, Color.FromArgb(a, avg, avg, avg));
                }
            pictureBox1.Image = bmp;

        }

        private void redToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                {
                    //get picture value
                    p = bmp.GetPixel(x, y);
                    //extract pixel component ARGB
                    int a = p.A;
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;
                    //set new value
                    bmp.SetPixel(x, y, Color.FromArgb(a, r, 0, 0));
                }
            pictureBox1.Image = bmp;
        }

        private void blueToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                {
                    //get picture value
                    p = bmp.GetPixel(x, y);
                    //extract pixel component ARGB
                    int a = p.A;
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;
                    //set new value
                    bmp.SetPixel(x, y, Color.FromArgb(a, 0, 0, b));
                }
            pictureBox1.Image = bmp;
        }

        private void greenToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            for (int y = 0; y < height; y++)
                for (int x = 0; x < width; x++)
                {
                    //get picture value
                    p = bmp.GetPixel(x, y);
                    //extract pixel component ARGB
                    int a = p.A;
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;
                    //set new value
                    bmp.SetPixel(x, y, Color.FromArgb(a, 0,g, 0));
                }
            pictureBox1.Image = bmp;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        public void SetGamma(double red, double green, double blue, Color c, PictureBox ptb)
        {
            
            Bitmap bmap = new Bitmap(ofdname);
            byte[] redGamma = CreateGammaArray(red);
            byte[] greenGamma = CreateGammaArray(green);
            byte[] blueGamma = CreateGammaArray(blue);
            for (int i = 0; i < bmap.Width; i++)
            {
                for (int j = 0; j < bmap.Height; j++)
                {
                    c = bmap.GetPixel(i, j);
                    bmap.SetPixel(i, j, Color.FromArgb(redGamma[c.R],
                       greenGamma[c.G], blueGamma[c.B]));
                }
            }
            ptb.Image = bmap;
        }

        private byte[] CreateGammaArray(double color)
        {
            byte[] gammaArray = new byte[256];
            for (int i = 0; i < 256; ++i)
            {
                gammaArray[i] = (byte)Math.Min(255,
        (int)((255.0 * Math.Pow(i / 255.0, 1.0 / color)) + 0.5));
            }
            return gammaArray;
        }


        private void gammaToolStripMenuItem_Click(object sender, EventArgs e)
        {

                    int a = p.A;
                    int r = p.R;
                    int g = p.G;
                    int b = p.B;
            SetGamma(r,g,b,p,pictureBox1);

        }

        public void SetBrightness(int brightness,PictureBox ptb)
        {
            Bitmap bmap = new Bitmap(ofdname);
            if (brightness < -255) brightness = -255;
            if (brightness > 255) brightness = 255;

            for (int i = 0; i < bmap.Width; i++)
            {
                for (int j = 0; j < bmap.Height; j++)
                {
                    p = bmap.GetPixel(i, j);
                    int cR = p.R + brightness;
                    int cG = p.G + brightness;
                    int cB = p.B + brightness;

                    if (cR < 0) cR = 1;
                    if (cR > 255) cR = 255;

                    if (cG < 0) cG = 1;
                    if (cG > 255) cG = 255;

                    if (cB < 0) cB = 1;
                    if (cB > 255) cB = 255;

                    bmap.SetPixel(i, j,
        Color.FromArgb((byte)cR, (byte)cG, (byte)cB));
                }
            }
            ptb.Image = bmap;
        }

        private void brightnessToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            SetBrightness(100,pictureBox1);
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(ofdname);
        }

        public void SetContrast(double contrast, PictureBox ptb)
        {

            if (ofdname == null)
            {
                MessageBox.Show("choose picture now ... !");
            }
            else
            {
                Bitmap bmap = new Bitmap(ofdname);
                if (contrast < -100) contrast = -100;
                if (contrast > 100) contrast = 100;
                contrast = (100.0 + contrast) / 100.0;
                contrast *= contrast;
                Color c;
                for (int i = 0; i < bmap.Width; i++)
                {
                    for (int j = 0; j < bmap.Height; j++)
                    {
                        c = bmap.GetPixel(i, j);
                        double pR = c.R / 255.0;
                        pR -= 0.5;
                        pR *= contrast;
                        pR += 0.5;
                        pR *= 255;
                        if (pR < 0) pR = 0;
                        if (pR > 255) pR = 255;

                        double pG = c.G / 255.0;
                        pG -= 0.5;
                        pG *= contrast;
                        pG += 0.5;
                        pG *= 255;
                        if (pG < 0) pG = 0;
                        if (pG > 255) pG = 255;

                        double pB = c.B / 255.0;
                        pB -= 0.5;
                        pB *= contrast;
                        pB += 0.5;
                        pB *= 255;
                        if (pB < 0) pB = 0;
                        if (pB > 255) pB = 255;

                        bmap.SetPixel(i, j,
            Color.FromArgb((byte)pR, (byte)pG, (byte)pB));
                    }
                }
                ptb.Image = bmap;
            }
        }
        private void contrastToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetContrast(100, pictureBox1);
        }

        public void SetInvert(PictureBox ptb)
        {
            
            Bitmap bmap = new Bitmap(ofdname);
            Color c;
            for (int i = 0; i < bmap.Width; i++)
            {
                for (int j = 0; j < bmap.Height; j++)
                {
                    c = bmap.GetPixel(i, j);
                    bmap.SetPixel(i, j,
      Color.FromArgb(255 - c.R, 255 - c.G, 255 - c.B));
                }
            }
            ptb.Image = bmap;
        }

        private void invertToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SetInvert(pictureBox1);
        }

        public void Resize(int newWidth, int newHeight, PictureBox ptb)
{
    	if (newWidth != 0 && newHeight != 0)
             {
                 Bitmap temp = new Bitmap(ofdname);
             Bitmap bmap = new Bitmap(newWidth, newHeight, temp.PixelFormat);

                    double nWidthFactor = (double)temp.Width / (double)newWidth;
                    double nHeightFactor = (double)temp.Height / (double)newHeight;

                    double fx, fy, nx, ny;
                    int cx, cy, fr_x, fr_y;
                    Color color1 = new Color();
                    Color color2 = new Color();
                    Color color3 = new Color();
                    Color color4 = new Color();
                    byte nRed, nGreen, nBlue;

                    byte bp1, bp2;

                    for (int x = 0; x < bmap.Width; ++x)
                    {
                          for (int y = 0; y < bmap.Height; ++y)
                        {

                                fr_x = (int)Math.Floor(x * nWidthFactor);
                                fr_y = (int)Math.Floor(y * nHeightFactor);
                                cx = fr_x + 1;
                                if (cx >= temp.Width) cx = fr_x;
                                cy = fr_y + 1;
                                if (cy >= temp.Height) cy = fr_y;
                                fx = x * nWidthFactor - fr_x;
                                fy = y * nHeightFactor - fr_y;
                                nx = 1.0 - fx;
                                ny = 1.0 - fy;

                                color1 = temp.GetPixel(fr_x, fr_y);
                                color2 = temp.GetPixel(cx, fr_y);
                                color3 = temp.GetPixel(fr_x, cy);
                                color4 = temp.GetPixel(cx, cy);

                                // Blue
                                bp1 = (byte)(nx * color1.B + fx * color2.B);

                                bp2 = (byte)(nx * color3.B + fx * color4.B);

                                nBlue = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                                // Green
                                bp1 = (byte)(nx * color1.G + fx * color2.G);

                                bp2 = (byte)(nx * color3.G + fx * color4.G);

                                nGreen = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                                // Red
                                bp1 = (byte)(nx * color1.R + fx * color2.R);

                                bp2 = (byte)(nx * color3.R + fx * color4.R);

                                nRed = (byte)(ny * (double)(bp1) + fy * (double)(bp2));

                                bmap.SetPixel(x, y, System.Drawing.Color.FromArgb
						(255, nRed, nGreen, nBlue));
                        }
                }
                    ptb.Image = bmap;
       }
}

        private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            
            Resize(sumW, subH, pictureBox1);
        }

        private void btnSum_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            
            sumW=sumW+width;
            sumH=sumH+height;
            MessageBox.Show("tang kich thuoc anh len "+ sumW.ToString() + "X" +sumH.ToString());
        }

        private void btnSub_Click(object sender, EventArgs e)
        {
            //load picture
            bmp = new Bitmap(ofdname);
            //get dimenssion
            width = bmp.Width;
            height = bmp.Height;
            
            subW =  width -100;
            subH =  height - 100;
            MessageBox.Show("giam kich thuoc anh  di 100x100");

        }

    }
}
